package com.coding.service.impl;

import com.coding.bean.Dept;
import com.coding.bean.ShiroUser;
import com.coding.constant.Constant;
import com.coding.dao.DeptMapper;
import com.coding.dao.UserMapper;
import com.coding.service.DeptService;
import com.coding.utils.JqgridUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper mapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JqgridUtil jqgridUtil;

    @Override
    public String getAllDeptJson(int page, int count) {
        List<Dept> all = mapper.selectList(null);
        int toIndex = count * page;
        if (all.size() < toIndex) {
            toIndex = all.size();
        }
        List<Dept> list = all.subList(count * (page - 1), toIndex);
        return jqgridUtil.getJson(list, page + "", all.size(), count);
    }

    @Override
    public List<Dept> queryAllDeptPage(int page, int count) {
        List<Dept> all = mapper.selectList(null);
        int toIndex = count * page;
        if (all.size() < toIndex) {
            toIndex = all.size();
        }
        return all.subList(count * (page - 1), toIndex);
    }

    @Override
    public String deptListForUserAdd() {
        List<Dept> list = mapper.selectList(null);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i).getId() + ":" + list.get(i).getName() + ";");
        }
        return sb.substring(0, sb.length() - 1);
    }

    @Override
    public String addDept(String deptName) {
        Dept temp = new Dept();
        temp.setName(deptName);
        Dept dept = mapper.selectOne(temp);
        if (dept != null) {
            return Constant.ERROR_ADD_DEPT_ALREADY_EXISTS;
        }
        dept = new Dept();
        dept.setName(deptName);
        int result = mapper.insert(dept);
        return result + "";
    }

    @Override
    public String modifyDept(String id, String deptName) {
        Dept dept = new Dept();
        dept.setId(Integer.parseInt(id));
        dept.setName(deptName);
        int result = mapper.updateById(dept);
        return result + "";
    }

    @Override
    public String deleteDept(String id) {
        if ("1".endsWith(id)) {
            return Constant.ERROR_CAN_NOT_DELETE_DEFAULT_DEPT;
        }
        ShiroUser updateUser = new ShiroUser();
        updateUser.setDeptid(1);
        updateUser.setId(Integer.parseInt(id));
        userMapper.updateById(updateUser);
        return mapper.deleteById(Integer.parseInt(id)) + "";
    }

    @Override
    public List<Dept> getAllDept() {
        return mapper.selectList(null);
    }
}
